"""
历史分析选品对话框
"""
from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QPushButton,
                           QLabel, QDoubleSpinBox, QSpinBox, QMessageBox)
from PyQt6.QtCore import Qt
from biz.script_runner import get_script_runner
from gui.widgets.history_analysis_auto_select_worker import HistoryAnalysisAutoSelectWorker

class HistoryAnalysisAutoSelectDialog(QDialog):
    """历史分析选品对话框"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
        self.setWindowTitle("设置自动选品条件")
        self.setFixedSize(400, 200)
        self.worker = None

    def setup_ui(self):
        """初始化UI"""
        layout = QVBoxLayout()
        layout.setSpacing(20)

        # 价格范围
        price_frame = QHBoxLayout()
        price_label = QLabel("价格范围:")
        self.price_min = QDoubleSpinBox()
        self.price_min.setRange(0, 999999)
        self.price_min.setDecimals(2)
        self.price_min.setValue(5.00)  # 设置最低价默认值为5
        price_separator = QLabel("-")
        self.price_max = QDoubleSpinBox()
        self.price_max.setRange(0, 999999)
        self.price_max.setDecimals(2)
        self.price_max.setValue(200.00)  # 设置最高价默认值为200

        price_frame.addWidget(price_label)
        price_frame.addWidget(self.price_min)
        price_frame.addWidget(price_separator)
        price_frame.addWidget(self.price_max)

        # 最低销售数量
        sales_frame = QHBoxLayout()
        sales_label = QLabel("最低销售数量:")
        self.sales_min = QSpinBox()
        self.sales_min.setRange(0, 999999)
        self.sales_min.setValue(500)  # 设置最小在售数量默认值为500

        sales_frame.addWidget(sales_label)
        sales_frame.addWidget(self.sales_min)
        sales_frame.addStretch()

        # 按钮区域
        button_frame = QHBoxLayout()
        self.start_btn = QPushButton("开始")
        self.cancel_btn = QPushButton("取消")
        
        self.start_btn.setStyleSheet("""
            QPushButton {
                padding: 5px 15px;
                background-color: #409eff;
                color: white;
                border: none;
                border-radius: 2px;
                min-width: 80px;
            }
            QPushButton:hover {
                background-color: #66b1ff;
            }
        """)
        
        self.cancel_btn.setStyleSheet("""
            QPushButton {
                padding: 5px 15px;
                background-color: #909399;
                color: white;
                border: none;
                border-radius: 2px;
                min-width: 80px;
            }
            QPushButton:hover {
                background-color: #a6a9ad;
            }
        """)

        button_frame.addStretch()
        button_frame.addWidget(self.start_btn)
        button_frame.addWidget(self.cancel_btn)

        # 添加所有组件到主布局
        layout.addLayout(price_frame)
        layout.addLayout(sales_frame)
        layout.addStretch()
        layout.addLayout(button_frame)

        self.setLayout(layout)

        # 连接信号
        self.start_btn.clicked.connect(self.accept)
        self.cancel_btn.clicked.connect(self.reject)

    def accept(self):
        """确认按钮点击事件"""
        try:
            # 获取筛选条件
            min_price = self.price_min.value()
            max_price = self.price_max.value()
            min_sales = self.sales_min.value()
            
            # 创建筛选条件
            filter_condition = {
                'price_min': min_price,
                'price_max': max_price,
                'sales_min': min_sales
            }
            
            # 停止脚本运行
            script_runner = get_script_runner()
            if script_runner:
                script_runner.stop_all()
            
            # 创建 worker 并返回给页面
            self.worker = HistoryAnalysisAutoSelectWorker(filter_condition)
            
            # 关闭对话框
            super().accept()
            
        except Exception as e:
            QMessageBox.warning(self, "错误", f"设置筛选条件失败: {str(e)}")

    def get_worker(self) -> HistoryAnalysisAutoSelectWorker:
        """获取创建的 worker"""
        return self.worker

    def get_settings(self):
        """获取设置的参数"""
        return {
            'price_min': self.price_min.value(),
            'price_max': self.price_max.value(),
            'sales_min': self.sales_min.value()
        }
